package wxk.interview.thread;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author wangxuekai
 * @version 2.2
 * @Date 2017/3/8.
 * @since 2.2
 */
public class MyThreadPool {

    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
        System.out.println(new Date()+"main run");
        for (int i = 0; i < 10; i++) {
//            pool.schedule(new Runnable() {
//                public void run() {
//                    long srt = System.currentTimeMillis();
//                    System.out.println(Thread.currentThread().getName() + "----开始执行啦");
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    long end = System.currentTimeMillis();
//                    System.out.println(Thread.currentThread().getName() + "-------执行结束啦,耗时:" + (end - srt) + "ms.");
//                }
//            },1, TimeUnit.SECONDS);

            pool.scheduleAtFixedRate(new Runnable() {
                public void run() {
                    System.out.println(new Date()+"------run");
                }
            },0,1,TimeUnit.SECONDS);
        }

        System.out.println(new Date()+"main out");

        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
